package com.yami.shop.consign.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.enums.ProdConsignStatus;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.consign.common.model.ProdConfig;
import com.yami.shop.consign.common.service.ConsignAnalysisService;
import com.yami.shop.consign.common.service.ProdConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

@RestController
@RequestMapping("/admin/consign/prod")
@Tag(name = "寄售商品配置（平台）")
@Slf4j
public class ConsignProdController {
    
    @Autowired
    private ProdConfigService prodConfigService;
    
    @Autowired
    private ConsignAnalysisService consignAnalysisService;

    @GetMapping("/page")
    @Operation(summary =  "获取寄售商品表列表", description = "获取寄售商品表列表")
    public ServerResponseEntity<PageVO<ProdConfig>> page(@Valid PageDTO pageDTO, ProdConfig prodConfig) {
        PageVO<ProdConfig> page = prodConfigService.getPage(pageDTO, prodConfig);
        return ServerResponseEntity.success(page);
    }

    @GetMapping("/getInfo")
    @Operation(summary =  "获取寄售商品详情", description = "获取寄售商品详情")
    public ServerResponseEntity<ProdConfig> getInfo(@RequestParam Long id) {
        ProdConfig byId = prodConfigService.getById(id);
        return ServerResponseEntity.success(byId);
    }
    
    @PostMapping
    @Operation(summary =  "保存寄售商品", description = "保存寄售商品")
    public ServerResponseEntity<Void> save(@RequestBody @Valid ProdConfig prodConfig){
        prodConfigService.insertProd(prodConfig);
        return ServerResponseEntity.success();
    }
    
    @PutMapping
    @Operation(summary =  "修改寄售商品", description = "修改寄售商品")
    public ServerResponseEntity<Void> update(@RequestBody @Valid ProdConfig prodConfig){
        ProdConfig one = prodConfigService.getOne(new LambdaQueryWrapper<ProdConfig>().eq(ProdConfig::getProdId, prodConfig.getProdId()).ne(ProdConfig::getId, prodConfig.getId()).ne(ProdConfig::getStatus, ProdConsignStatus.DELETE.getValue()));
        if (Objects.nonNull(one)) {
            throw new YamiShopBindException("该商品已参与寄售！");
        }
        prodConfigService.updateById(prodConfig);
        if (prodConfig.getStatus() == ProdConsignStatus.OPEN.getValue()) {
            consignAnalysisService.updateClosingPrice(prodConfig.getProdId());
        }
        return ServerResponseEntity.success();
    }
    
    
    @PutMapping("/updateStatus")
    @Operation(summary =  "修改寄售商品状态", description = "修改寄售商品状态")
    public ServerResponseEntity<Void> updateStatus(@RequestBody ProdConfig prodConfig){
        ProdConfig byId = prodConfigService.getById(prodConfig.getId());
        byId.setStatus(prodConfig.getStatus());
        prodConfigService.updateById(byId);
        if (prodConfig.getStatus() == ProdConsignStatus.OPEN.getValue()) {
            consignAnalysisService.updateClosingPrice(byId.getProdId());
        }
        return ServerResponseEntity.success();
    }
    
    
    @DeleteMapping
    @Operation(summary =  "批量删除", description = "批量删除")
    public ServerResponseEntity<Void> delete(@RequestParam Long[] ids){
        prodConfigService.batchDelete(ids);
        return ServerResponseEntity.success();
    }

    @GetMapping("/getByProdId")
    @Operation(summary =  "获取寄售商品详情", description = "获取寄售商品详情")
    public ServerResponseEntity<ProdConfig> getByProdId(@RequestParam Integer prodId) {
        ProdConfig one = prodConfigService.getOne(new LambdaQueryWrapper<ProdConfig>().eq(ProdConfig::getProdId, prodId).ne(ProdConfig::getStatus, ProdConsignStatus.DELETE.getValue()));
        return ServerResponseEntity.success(one);
    }
    
    
}
